package Stream;

import Lambda.Employee;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Demo {

    private List<Employee> employeeList=null;

    @Before
    public void init(){
        employeeList=new ArrayList<>();
        employeeList.add(new Employee(1,"张三", 15,8000,"销售部"));
        employeeList.add(new Employee(2,"李四", 35,6000,"客服"));
        employeeList.add(new Employee(3,"王五", 25,10000,"财务部"));
        employeeList.add(new Employee(4,"赵六", 35,11000,"财务部"));
        employeeList.add(new Employee(5,"田七", 22,12000,"管理部"));
        employeeList.add(new Employee(6,"老八", 26,15000,"技术部"));
        employeeList.add(new Employee(7,"三炮", 27,9000,"技术部"));
        employeeList.add(new Employee(8,"李九", 29,11000,"管理部"));
        employeeList.add(new Employee(9,"周十", 48,18000,"技术部"));
    }

    @Test
    public void test1(){

        //中间操作，不会执行任何操作
        Stream<Employee> employeeStream = employeeList.stream().filter((employee) -> employee.getAge() >= 35);
        //终止操作: 一次性执行全部操作。即“惰性求值”。
        employeeStream.forEach(System.out::println);

        Stream<Double> randomStream=Stream.generate(Math::random).limit(5);
        randomStream.forEach(System.out::println);

        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8).skip(2);
        integerStream.forEach(System.out::println);

        Stream.of("abc","d","e","abc").distinct().forEach(System.out::println);
    }

    @Test
    public void test2(){
        IntStream stream = Arrays.stream(new int[]{1, 32, 45, 3});
    }

    @Test
    public void test3(){
        Stream<String> stringStream = Stream.of("ab", "ac", "bc");
    }

    @Test
    public void test4(){
        Stream<Integer> iterate = Stream.iterate(0, integer -> integer*2);

        // 这里会一直调用generate提供的Supplier生成下一个元素.
        Stream<Double> generate = Stream.generate(() -> Math.random());

        generate.limit(10).forEach(System.out::println);

    }


}
